//
//  MultiFilterContentView.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/7/3.
//

import UIKit
import SnapKit

protocol MultifilterContent {
  var text: String { get }
}

class MultifilterContentView<Content: MultifilterContent>: UIView {

  private lazy var wrapperStack = UIStackView(arrangedSubviews: [])
  private var layout: FilterSelectedView.Layout = .center

  var datasource: [Content] = [] {
    didSet {
      for (index, content) in datasource.enumerated() {
        let item = FilterSelectedView(frame: .zero, layout: layout)
        item.text = content.text
        item.button.tag = index + 10
        item.button.addTarget(self, action: #selector(tapped(sender:)), for: .touchUpInside)
        wrapperStack.addArrangedSubview(item)
      }
    }
  }

  func update(_ content: Content, at index: Int) {
    guard index < datasource.count,
          index < wrapperStack.arrangedSubviews.count
    else { return }

    let item = wrapperStack.arrangedSubviews[index] as! FilterSelectedView
    item.text = content.text
    datasource[index] = content
  }

  @objc func tapped(sender: UIButton) {
    let index = sender.tag - 10
    guard index < datasource.count else {
      return
    }
  }

  init(frame: CGRect, layout: FilterSelectedView.Layout = .center) {
    super.init(frame: frame)
    self.layout = layout
    wrapperStack.axis = .horizontal
    wrapperStack.distribution = .fillEqually
    addSubview(wrapperStack)

    wrapperStack.snp.makeConstraints {
      $0.edges.equalToSuperview()
    }
  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}


class FilterSelectedView: UIView {

  enum Layout {
    case center
    case leading
    case trailing
  }

  private var label: UILabel = {
    let lb = UILabel(frame: .zero)
    lb.font = .designKit.body
    lb.textColor = .designKit.primaryText
    return lb
  }()
  private var imageView = UIImageView(image: R.image.icon_arraw_down())
  private lazy var wrapperStack = UIStackView(arrangedSubviews: [label, imageView])

  var button = UIButton(type: .custom)
  var text: String = "" {
    didSet {
      label.text = text
    }
  }

  init(frame: CGRect, layout: Layout = .center) {
    super.init(frame: frame)

    backgroundColor = .designKit.background

    wrapperStack.axis = .horizontal
    wrapperStack.spacing = 4
    addSubview(wrapperStack)
    addSubview(button)

    button.snp.makeConstraints {
      $0.edges.equalToSuperview()
    }

    switch layout {
    case .center:
      wrapperStack.snp.makeConstraints {
        $0.center.equalToSuperview()
      }
    case .leading:
      wrapperStack.snp.makeConstraints {
        $0.centerY.equalToSuperview()
        $0.leading.equalToSuperview().offset(15)
      }
    case .trailing:
      wrapperStack.snp.makeConstraints {
        $0.centerY.equalToSuperview()
        $0.trailing.equalToSuperview().offset(-15)
      }
    }
  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}
